﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditTrade : Form
    {
        string sExecutionID;
        DateTime tradeDate;

        public EditTrade(string sExecutionID, DateTime tradeDate)
        {
            InitializeComponent();

            this.sExecutionID = sExecutionID;
            this.tradeDate = tradeDate;
        }

        private void EditTrade_Load(object sender, EventArgs e)
        {
            try
            {
	            DailyTransactions transaction = DailyTransactionsServices.GetDailyTransactionsByExecIdAndDate(sExecutionID, tradeDate);
	
	            labelExecutionID.Text = sExecutionID;
	            labelTradeDate.Text = tradeDate.ToLongDateString();
	
	            textBoxClientCode.Text = transaction.ClientCode;
	            textBoxSymbol.Text = transaction.CompanySymbol;
	            textBoxQuantity.Text = transaction.Quantity.ToString();
	            textBoxPrice.Text = transaction.Price.ToString();
	            textBoxAmount.Text = transaction.Amount.ToString();
	            if (transaction.BuySell == "B")
	            {
	                comboBoxSide.Text = "BUY";
	            }
	            else if (transaction.BuySell == "S")
	            {
	                comboBoxSide.Text = "SELL";
	            }
	
	            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
	
	            ClientCollection clientCollection = ClientService.GetClientCollection();
	
	            for (int iI = 0; iI < clientCollection.Count; iI++)
	            {
	                data.Add(clientCollection[iI].Code);
	            }
	
	            textBoxClientCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
	            textBoxClientCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
	            textBoxClientCode.AutoCompleteCustomSource = data;
	
	            AutoCompleteStringCollection data2 = new AutoCompleteStringCollection();
	
	            SecurityCollection securityCollection = SecurityServices.GetSecurityCollection();
	
	            for (int iI = 0; iI < securityCollection.Count; iI++)
	            {
	                data2.Add(securityCollection[iI].Symbol);
	            }
	
	            textBoxSymbol.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
	            textBoxSymbol.AutoCompleteSource = AutoCompleteSource.CustomSource;
	            textBoxSymbol.AutoCompleteCustomSource = data2;
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            DailyTransactions transaction = new DailyTransactions();
            transaction.ExecutionId = sExecutionID;
            transaction.TradeDate = tradeDate;
            transaction.ClientCode = textBoxClientCode.Text;
            transaction.CompanySymbol = textBoxSymbol.Text;
            transaction.Quantity = int.Parse(textBoxQuantity.Text);
            transaction.Price = double.Parse(textBoxPrice.Text);
            transaction.Amount = double.Parse(textBoxAmount.Text);

            if (comboBoxSide.Text == "BUY")
            {
                transaction.BuySell = "B";
            }
            else
            {
                transaction.BuySell = "S";
            }

            try
            {
                DailyTransactionsServices.Update(transaction);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update transaction information\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Transaction information updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            if (textBoxClientCode.Text == ""
                || textBoxSymbol.Text == ""
                || textBoxQuantity.Text == ""
                || textBoxPrice.Text == ""
                || textBoxAmount.Text == ""
                || comboBoxSide.Text == "")
            {
                MessageBox.Show("Enter necessary details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fFloatNum = 0;
            if (double.TryParse(textBoxPrice.Text, out fFloatNum) == false)
            {
                MessageBox.Show("Invalid price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (double.TryParse(textBoxAmount.Text, out fFloatNum) == false)
            {
                MessageBox.Show("Invalid amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            int iIntNum = 0;
            if (int.TryParse(textBoxQuantity.Text, out iIntNum) == false)
            {
                MessageBox.Show("Invalid quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void textBoxClientCode_Enter(object sender, EventArgs e)
        {
            AcceptButton = null;
        }

        private void textBoxClientCode_Leave(object sender, EventArgs e)
        {
            AcceptButton = buttonOK;
        }

        private void textBoxSymbol_Enter(object sender, EventArgs e)
        {
            AcceptButton = null;
        }

        private void textBoxSymbol_Leave(object sender, EventArgs e)
        {
            AcceptButton = buttonOK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
